<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="品质改善编号" prop="qualityImproveCode">
        <el-input
          v-model="queryParams.qualityImproveCode"
          placeholder="请输入品质改善编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="dictQualityImproveStatus">
        <el-select
          v-model="queryParams.dictQualityImproveStatus"
          placeholder="请选择状态"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="(dict, index) in getIntDictOptions('dict_quality_improve_status')"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="故障类别" prop="dictFaultType">
        <el-select
          v-model="queryParams.dictFaultType"
          placeholder="请选择故障类别"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="(dict, index) in getIntDictOptions('dict_fault_type')"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="operateClick">
      <div>
        <el-button
          v-hasPermi="['quality:improvementHandle:export']"
          type="primary"
          :loading="exportLoading"
          @click="handleExport"
        >
          数据导出
        </el-button>
      </div>
      <div class="titleSet">
        <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
      </div>
    </div>
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      @register="tableoperation"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :tableHeight="tableHeight"
    >
      <template #action="{ row }">
        <!-- 10 编辑中 20 待确认 30 待处理 40 已驳回 50 已完成 60 已作废 -->
        <el-button
          v-hasPermi="['quality:improvementHandle:update']"
          v-if="[30, 40].includes(row.dictQualityImproveStatus)"
          link
          type="primary"
          @click="openForm('update', row.id)"
        >
          编辑
        </el-button>
        <el-button
          v-hasPermi="['quality:improvementHandle:view']"
          link
          type="primary"
          @click="openForm('view', row.id)"
        >
          查看
        </el-button>
      </template>
    </Table>
  </ContentWrap>

  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getLabelAll" />
</template>

<script setup lang="ts" name="QualityImprovementHandle">
import { getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import * as ImprovementApi from '@/api/quality/qualitycontrol/improvementHandle'

const tableTitleList = ref([]) // 表头的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type: 2,
  qualityImproveCode: null,
  productCode: null,
  dictFaultType: undefined,
  dictQualityImproveStatus: undefined,
  effectVerify: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const { tableObject, tableMethods } = useTable({
  getListApi: ImprovementApi.getImprovementPageHandle, // 分页接口
  defaultParams: queryParams
})

/** 查询列表 */
const getLabelAll = async () => {
  try {
    const data = await ImprovementApi.getImprovementPageHandle(queryParams)
    let json = {
      label: '操作',
      field: 'action',
      isDetail: false,
      width: 110
    }
    data.labels.push(json)
    const { allSchemas } = useCrudSchemas(data.labels)
    tableTitleList.value = allSchemas.tableColumns as any
  } finally {
  }
}

// 获得表格的各种操作
const { getList } = tableMethods

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/**表头内部方法 */
const usTableoperation = ref()
const tableoperation = (parent: any) => {
  usTableoperation.value = parent
}

/** 添加/修改操作 */
const { push } = useRouter() // 路由
const openForm = (type: string, id?: number) => {
  push({
    path: '/quality/quality/qualitycontrol/ImprovementHandleEventForm',
    query: {
      id,
      type
    }
  })
}

/**打开表格表头设置**/
const tableTitleSet = ref()
const openTableTitleSet = () => {
  let code = 'QualityImprovementHandle'
  tableTitleSet.value.open(tableTitleList.value, code)
}

/** 导出按钮操作 */
const exportRef = ref()
const handleExport = () => {
  exportRef.value.open()
}
// 导出
const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
    fileName: val.name,
    type: 2,
    ids: ''
  }
  if (val.range === 'all') {
    params.ids = ''
  } else {
    params.ids = usTableoperation.value.selections.map((item) => item.id).join(',')
  }
  ImprovementApi.exportImprovement(params).then((res) => {
    download.excel(res, val.name)
    clearSelectData()
  })
}

//清空多选
const clearSelectData = () => {
  usTableoperation.value.selections = []
  usTableoperation.value.clearTableSelect()
}

/** 初始化 **/
const tableHeight = ref(0)
onMounted(async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
  tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
  await getLabelAll()
  getList()
})
</script>
<style lang="scss" scoped>
.operateClick {
  height: 50px;
  display: flex;
  justify-content: space-between;
  .titleSet {
    display: flex;
    justify-content: flex-end;
  }
}

:deep(.el-select .el-input) {
  width: 100%;
}
</style>
